using UnityEngine;
using System.Collections;

public class HudScript : MonoBehaviour {
	
	public Texture2D lifeIcon;
	public Texture2D bulletIcon;
	
	public Texture2D noAmmoIcon; // TODO ?
	public Texture2D reloadBulletIcon; // TODO ?
	public Texture2D jamBulletIcon; // TODO ?
	public Texture2D deadBG;
	CharacterManager manager;
	public Texture2D powerUpIcon;
	
	// track values
	int numberOfLives;
	int numberOfBullets;
	int score;
	bool isPoweredUp;
	
	bool isReloading;
	bool isJammed;
	
	// life head
	const int headOffX = 10;
	const int headOffY = 10;
	const int headWidth = 40;
	const int headHeight = 40;
	
	// bullets
	const int bulletOffX = headOffX + headWidth + 10;
	const int bulletOffY = 10;
	const int bulletWidth = 35;
	const int bulletHeight = 35;
	const int bulletsPerRow = 10;
	
	// power up
	const int powerOffX = 150;
	const int powerOffY = 30;
	const int powerWidth = 50;
	const int powerHeight = 50;
	
	// Use this for initialization
	void Start () {
		// ...
		manager = GenerateObjects.getPlayer().GetComponent<CharacterManager>();
	}
	
	// Update is called once per frame
	void Update () {
		// ...
		// TODO pull values?
		this.numberOfLives = manager.playerLives;
		score = manager.score;
		// TODO is powered up>
	}
	
	/**
	 * Posts total bullets in clip for hud
	 */
	public void postBullets( int totalBullets, bool isJammed, bool isReload ) {
		this.numberOfBullets = totalBullets;
		this.isReloading = isReload;
		this.isJammed = isJammed;
	}
	
	void OnGUI() {
		if (!manager.dead) {
			// life heads
			GUIContent lifeContent = new GUIContent( lifeIcon );
			for (int i=0; i < numberOfLives; i++) {
				GUI.Box (new Rect ( 
					headOffX,
					headOffY + (i * headHeight), 
					headWidth, 
					headHeight ), lifeContent );
			}
			
			// bullets
			GUIContent bulletContent;
			if (isJammed) {
				bulletContent = new GUIContent( jamBulletIcon );
			}
			else if (isReloading) {
				bulletContent = new GUIContent( reloadBulletIcon );
			}
			else if (numberOfBullets == 0) {
				bulletContent = new GUIContent( noAmmoIcon );
			}
			else {
				bulletContent = new GUIContent( bulletIcon );
			}
			
			for (int i=0; i < numberOfBullets; i++) {
				int row = i / bulletsPerRow;
				int realCount = i - (row * bulletsPerRow);
				
				GUI.Box( new Rect( 
					bulletOffX + (bulletWidth * realCount),
					bulletOffY + (bulletHeight * row),
					bulletWidth,
					bulletHeight ), bulletContent );
			}
			
			// powered up
			if (isPoweredUp) {
				GUI.Box (new Rect ( 
					powerOffX,
					powerOffY, 
					powerWidth, 
					powerHeight ), new GUIContent( powerUpIcon ));
			}
		}
		else {
			GUI.Label(new Rect(0,0,Screen.width, Screen.height), deadBG);
			if (GUI.Button(new Rect(Screen.width-Screen.width*.25f, Screen.height-Screen.height*.1f, Screen.width*.25f, Screen.height*.1f), "RESTART")) {
				manager.dead= false;
				manager.Restart();
			}
				
		}
		
		// TODO score?
	}
}
